package com.wwwzh.himalayafm.adapter.live_adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.callback.IsScrollButtonListener;
import com.wwwzh.himalayafm.entity.entity_broadtwo.BroadResult;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by wuruyuan on 2016/12/7.
 */

public class BroadAdapter extends RecyclerView.Adapter<BroadAdapter.ViewHolder>{

private Context context;
    private List<BroadResult> results;
private IsScrollButtonListener listener;
    public BroadAdapter(Context context,IsScrollButtonListener listener) {
        this.context = context;
        this.listener=listener;
        results=new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.broad_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

            holder.title.setText(results.get(position).getRname());
            holder.count.setText(results.get(position).getRadioPlayCount());
            loadImage(holder.image, results.get(position).getRadioCoverLarge());


        if (position==results.size()-1) {
            Log.d("vvvv","aaaa");
            listener.isScrollbutton();
        }
    }
    private void loadImage(ImageView image, String coverLarge) {
        Picasso.with(image.getContext())
                .load(coverLarge)
                .into(image);
    }
    public void addAll(Collection<? extends BroadResult> top) {
        int size = results.size();
        results.addAll(top);
        Log.d("vvvv","top.size():"+top.size());
        notifyItemRangeInserted(size, top.size());
    }
    @Override
    public int getItemCount() {
        return results.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView count;
        private ImageView image;
        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.broad_tv_title);
            count = (TextView) itemView.findViewById(R.id.broad_tv_count);
            image= (ImageView) itemView.findViewById(R.id.broad_image_hh);
        }
    }


}
